package com.jyy.prefabricated.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jyy.prefabricated.entity.StudentAchievement;
import com.jyy.prefabricated.enums.EducationBackgroundStatusEnum;
import com.jyy.prefabricated.vo.AchievementData;

import java.util.ArrayList;
import java.util.List;

public class AchievementExcelListener extends AnalysisEventListener<AchievementData> {

    // 因为最终保存数据库的操作是在本类中实现，所以要添加一个属性，使用UserCreditRecordService里的CRUD方法

    private Integer excelNum = 0;
    private Integer successNum = 0;
    /**
     * 学员信息
     */
    List<StudentAchievement> studentAchievements = new ArrayList<>();

    /**
     * 无参构造
     */
    public AchievementExcelListener() {
    }

    public List<StudentAchievement> getStudentAchievements() {
        return studentAchievements;
    }

    public void setStudentAchievements(List<StudentAchievement> studentAchievements) {
        this.studentAchievements = studentAchievements;
    }

    public Integer getExcelNum() {
        return excelNum;
    }

    public void setExcelNum(Integer excelNum) {
        this.excelNum = excelNum;
    }

    public Integer getSuccessNum() {
        return successNum;
    }

    public void setSuccessNum(Integer successNum) {
        this.successNum = successNum;
    }

    //easyExcel每次从Excel中读取一行数据就会调用一次invoke方法
    @Override
    public void invoke(AchievementData schievementData, AnalysisContext analysisContext) {
        if (schievementData != null && schievementData.getIdCard() != null) {
            StudentAchievement studentAchievement = new StudentAchievement();
            if (schievementData.getGradeTwo() != null) {
                if ("合格".equals(schievementData.getGradeTwo())) {
                    studentAchievement.setGradeTwo(1);
                }
                if ("不合格".equals(schievementData.getGradeTwo())) {
                    studentAchievement.setGradeTwo(0);
                }
            }
            studentAchievement.setIdCard(schievementData.getIdCard());
            if (schievementData.getCategoryId()!= null) {
                studentAchievement.setCategoryName(schievementData.getCategoryId());
            }
            studentAchievements.add(studentAchievement);
        }
    }

    //easyExcel在将Excel表中数据读取完毕后，最终执行此方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //最后,如果size<BATCH_COUNT就在这里进行数据的处理

    }

}
